//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.framework.web.codetable;

import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

public class SpringBeanUtil implements BeanFactoryAware {
    private static ServletContext sc = null;
    private static BeanFactory beanFactory;
    private static WebApplicationContext wctx;
    private static WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    private static ApplicationContext app;
    private static Logger logger = Logger.getLogger(SpringBeanUtil.class);

    public SpringBeanUtil() {
    }

    public static void setServletContext(ServletContext sc) {
        SpringBeanUtil.sc = sc;
        wctx = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
    }

    public static Object getBean(String beanName) {
        if (wac != null && wac.containsBean(beanName)) {
            return wac.getBean(beanName);
        } else if (wctx != null && wctx.containsBean(beanName)) {
            return wctx.getBean(beanName);
        } else {
            return app != null && app.containsBean(beanName) ? app.getBean(beanName) : beanFactory.getBean(beanName);
        }
    }

    public static <T> T getBean(String beanName, Class<T> clazs) {
        return clazs.cast(getBean(beanName));
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        SpringBeanUtil.beanFactory = beanFactory;
    }

    public static void showBeanNames() {
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        ServletContext var1 = wac.getServletContext();

        try {
            WebApplicationContext wctx = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
            String[] beans = wctx.getBeanDefinitionNames();
            String[] var7 = beans;
            int var6 = beans.length;

            for(int var5 = 0; var5 < var6; ++var5) {
                String bean = var7[var5];
                logger.warn(bean);
            }
        } catch (Exception var8) {
        }

    }

    public static BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        app = applicationContext;
    }
}
